# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
# Contributor: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=kicad
pkgver=8.0.1
pkgrel=3
pkgdesc="Cross Platform and Open Source Electronics Design Automation Suite"
url="https://www.kicad.org/"
# No support for s390x/riscv64 in libcontext (shipped with kicad)
# 32-bit arm: memory alignment access error in tests
arch="all !armhf !armv7 !riscv64 !s390x"
license="GPL-3.0-or-later"
makedepends="
	boost-dev
	cairo-dev
	chrpath
	cmake
	curl-dev
	glew-dev
	glm-dev
	glu-dev
	libgit2-dev
	mesa-dev
	ngspice-dev
	opencascade-dev
	py3-wxpython
	python3-dev
	samurai
	swig
	unixodbc-dev
	wxwidgets-dev
	zlib-dev
	"
checkdepends="
	py3-pytest
	"
depends="
	python3
	py3-cairosvg
	"
subpackages="
	$pkgname-doc
	"
source="
	https://gitlab.com/kicad/code/kicad/-/archive/$pkgver/kicad-$pkgver.tar.gz

	0001-opencascade-7.8.0.patch
	"

provides="$pkgname-opencascade=$pkgver-r$pkgrel"
replaces="$pkgname-opencascade"

build() {
	# XXX: GLIBCXX_ASSERTIONS causes link errors and symbols are missing
	# -DKICAD_STDLIB_LIGHT_DEBUG also enables this, maybe that works instead
	CFLAGS="$CFLAGS -O2" \
	CXXFLAGS="$CXXFLAGS -U_GLIBCXX_ASSERTIONS -O2" \
	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DKICAD_USE_EGL=ON \
		-DKICAD_SCRIPTING_WXPYTHON=OFF \
		-DOPENGL_opengl_LIBRARY=/usr/lib/libGL.so
	# build order workaround
	cmake --build build --target common/pcb_lexer.h
	cmake --build build
}

check() {
	local failing_tests
	case "$CARCH" in
	armv7|armhf|mips|x86)
		# qa_cli requires too much memory to pass on 32bit arches
		failing_tests="qa_cli"
		;;
	ppc64le)
		# design rule regression checks fail on ppc64le
		failing_tests="qa_pcbnew"
		;;
	*)
		failing_tests=""
	esac
	ctest --test-dir build --output-on-failure -E $failing_tests
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	local sitedir=$(python3 -c "import site; print(site.getsitepackages()[0])")
	chrpath -d "$pkgdir/$sitedir"/_pcbnew.so
}

doc() {
	default_doc

	amove usr/share/kicad/demos
}

sha512sums="
c38ed453a6a1fb49ab3a9b538789680aeff6f927f9a98367b037149b8d9b857060170cdb1ecf59bc5f434fed61b6cba0c260c6eb2a12936ee7fdd7ce47042f0a  kicad-8.0.1.tar.gz
efa52554f11bc3ec6e37a4ae054a4dbe64c71b82ec50b3de24be9353429d8ba2e1eea514cccbb5bc87bccdd78495a24f340275cc51c619b22dd5066e0d8b6f3b  0001-opencascade-7.8.0.patch
"
